package com.example.pulseanalyse.dealRealPointBLE;



import com.example.hyg_veintype.utils.chartUtil.RealTimeChartUtils;

import java.util.LinkedList;

/**
 * 蓝牙接受数据处理（基本与前端一致）
 */
public class PointUtils {
    //存放实时动压的队列(初始数据)
    LinkedList<Integer> queueData = new LinkedList<>();
    public LinkedList<Integer> drawPointY = new LinkedList<>();
    LinkedList<Integer> baseData = new LinkedList<>();
    // 王工
    int Flt;
    //队列数据和
    int nHe;
    //队列头旧值
    int nOld;
    //基线的高度  
    int nBLHeigt;
    //0x0bb8
    int nSclNum;
    //起点X坐标
    public int xValue=1478;

    public PointUtils() {
        //存放实时动压的队列(初始数据)
        queueData.clear();
        drawPointY.clear();
        baseData.clear();
        Flt = 120;
        //队列中数据和
        nHe = 270 * 100;
        //新的值
        nOld = 270;
        //基线的高度  
        nBLHeigt = 120;
        //0x0bb8  可以控制波幅大小
        nSclNum = 3500;
        //初始化一条长度1478Y值50的直线
        for (int i = 0; i < RealTimeChartUtils.POINT_MAX; i++) {
            queueData.add(50);
            drawPointY.add(50);
            baseData.add(50);
        }
    }

    /**
     * 是否是设备采集用到的压力数据
     *
     * @param bytes （头部第四位判断）
     * @return
     */
    public boolean isPressureData(byte[] bytes) {
        if (bytes != null && bytes.length > 3 && bytes[3] == 0x8) {
            return true;
        }
        return false;
    }

    /**
     * 获得实时静压
     */
    public int getStaticPressure(byte[] bytes) {
        int pulseStaticPressureValue = (bytes[4] << 8) | bytes[5];
        return pulseStaticPressureValue;
    }

    /**
     * 处理蓝牙接受的byte数据转换成点的Y坐标值（动压）
     */
    public synchronized int getDynamicPressure(byte[] bytes) {

        //动压
        int receveData=(int) ((((byte)0 & 0xFF)<<24)
                |((bytes[6] & 0xFF)<<16)
                |((bytes[7] & 0xFF)<<8)
                |(bytes[8] & 0xFF));

        if (bytes[12] == 1) {
            receveData = -receveData;
        }
        queueData.poll();
        queueData.add(receveData);

        drawPointY = queueData;

        nHe = nHe + drawPointY.get(drawPointY.size() - 1) - nOld;
        nOld = drawPointY.get(drawPointY.size() - Flt);
        //队列（长度）的平均值  Y值
        int nTmp = receveData - (nHe / (Flt + 1));
        //（在基线上）Y值调整
        int resultValue = nTmp / nSclNum + nBLHeigt;
        //最大不超过图表设置的Y值最大值
        if (resultValue > RealTimeChartUtils.POINT_MAX) {
            resultValue = 334;
        } else if (resultValue < 1) {
            resultValue = 1;
        }
        baseData.poll();
        baseData.add(resultValue);

        drawPointY = baseData;
        xValue=xValue+1;

        return resultValue;
    }

}
